package services.common;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import models.common.entity.t_bank_card_user;
import models.common.entity.t_bank_card_user.Status;
import models.common.entity.t_bank_list;
import models.common.entity.t_city;
import models.common.entity.t_user_info;

import org.apache.commons.lang.StringUtils;

import services.base.BaseService;

import common.utils.Factory;
import common.utils.LoggerUtil;
import common.utils.PageBean;
import common.utils.ResultInfo;

import daos.common.BankCardUserDao;

/**
 * 用户银行卡信息service实现
 * 
 * @description 
 *
 * @author ChenZhipeng
 * @createDate 2015年12月23日
 */
public class BankCardUserService extends BaseService<t_bank_card_user> {

	private BankCardUserDao bankCardUserDao = Factory.getDao(BankCardUserDao.class);
	
	protected BankCardUserService() {
		super.basedao = this.bankCardUserDao;
	}
	protected static BankListService bankListService = Factory.getService(BankListService.class);
	protected static CityService cityService = Factory.getService(CityService.class);
	
	protected static UserInfoService userInfoService = Factory.getService(UserInfoService.class);
	/**
	 * 查询用户银行卡列表
	 *
	 * @param pageSize
	 * @param currPage
	 * @param userId
	 * @return
	 *
	 * @author Chenzhipeng
	 * @createDate 2015年12月23日
	 */
	public PageBean<t_bank_card_user> pageOfUserCard(int currPage,
			int pageSize, long userId) {
		
		return bankCardUserDao.pageOfUserCard(currPage, pageSize, userId);
	}

	/**
	 * 判断一个银行卡号是否已经存在，存在则返回true，不存在返回false
	 * @param bankAccount
	 * @return
	 *
	 * @author YanPengFei
	 * @createDate 2016年08月01日
	 */
	public boolean isBankAccountExists(String bankAccount) {
		t_bank_card_user bankCardUser = bankCardUserDao.findBankCardUserByBankAccount(bankAccount);
		
		if (bankCardUser != null) {
			
			return true;
		}
		
		return false;
	}
	
	/**
	 * 添加用户银行卡
	 *                               
	 * @param userId          用户Id           
	 * @param branch_name      银行支行名称          
	 * @param bankCode             银行code        
	 * @param accountbank_num        账号或卡号          
	 * @param regional_name     所在省           
	 * @param city_name       所在市
	 * @param back_user_name		 用户真实姓名
	 * @param bankName			开户银行
	 * @param bankPreMobile 银行卡预留手机号码
	 * @return
	 * @author Liangqiongfeng
	 *@version 2018-3-29
	 */
	public boolean addUserCard(long userId, String branch_name , String bankCode, String accountbank_num,String regional_name,String city_name,
			String back_user_name,String bankName, String bankPreMobile) {
		t_bank_card_user.Status status = t_bank_card_user.Status.TYPE_DEFAULT;
		
		t_bank_card_user bankCard = bankCardUserDao.findByColumn("user_id = ?", userId);
		if (bankCard == null) {
			
			bankCard = new t_bank_card_user();
		}
		
		bankCard.user_id = userId;
		bankCard.bank_name = bankName;
		bankCard.bank_code = bankCode;
		bankCard.account = accountbank_num;
		bankCard.time = new Date();
		bankCard.regional_name = regional_name;
		bankCard.city_name = city_name;
		bankCard.back_user_name = back_user_name;
		bankCard.branch_name = branch_name;
		bankCard.bankPreMobile = bankPreMobile;
		bankCard.setStatus(status);
		
		return bankCardUserDao.save(bankCard);
	}
	/**
	 * 添加银行卡
	 *
	 * @param bank_type
	 * @param bank_name
	 * @param bank_num
	 * @param regionalId
	 * @param cityId
	 * @param user_name
	 * @param is_company
	 * @return
	 * @author xiaojun
	 *@version 2017-11-29
	 * @param bankPreMobile 银行卡预留手机号码 
	 */
	public ResultInfo saveBankCard(long bank_type, String bank_name, String bank_num,long regionalId,long cityId,String user_name,int is_company,long userId, String bankPreMobile){
		ResultInfo result = new ResultInfo();
		if (bank_type < 0) {
			result.code = -1;
			result.msg = "请选择开户银行";
			return result;
		}
		
		if (StringUtils.isBlank(bank_name)) {
			result.code = -1;
			result.msg = "请填写支行名称";
			return result;
		}
		
		if (StringUtils.isBlank(bank_num)) {
			result.code = -1;
			result.msg = "请填写银行卡号";
			return result;
		}
		if (StringUtils.isBlank(user_name)) {
			result.code = -1;
			result.msg = "请填写开户人姓名";
			return result;
		}
		//获取银行信息
		t_bank_list bank = null;
		if(bank_type != 0){
			bank = bankListService.findByID(bank_type);
		}
		//银行卡信息的校验；获取银行卡信息
		if (bank_type != 0 && bank == null) {
			result.code = -1;
			result.msg = "当前选的银行信息不存在";
			return result;
		}
		if (bank != null && bank.bank_no != null && !Arrays.asList(bank.bank_no.split(";")).contains(bank_num.subSequence(0, 6))) {
			result.code = -1;
			result.msg = "银行卡号和对应的银行不一致";
			return result;
		}
		
		if( is_company == 0){
			t_user_info  user_info = userInfoService.findUserInfoByUserId(userId);
			if(!user_name.equals(user_info.reality_name)){
				result.code = -1;
				result.msg = "请使用本人银行卡绑定！";
				return result;
			}
		}
		/*if (isBankAccountExists(bank_num)){
			result.code = 1;
			result.msg = "银行卡号已经存在";
			return result;
		}*/
		t_city city = cityService.findByID(cityId);
		t_city provint = cityService.findByID(regionalId);

		boolean flag = false;
		
		if(bank_type != 0 && bank != null){
			
			flag = addUserCard(userId, bank_name, bank.code, bank_num,provint.name,city.name,user_name,bank.name,bankPreMobile);
		}else{
			flag = addUserCard(userId, bank_name,null, bank_num,provint.name,city.name,user_name,bank_name,bankPreMobile);
		}
		
		if (!flag) {
			LoggerUtil.error(true, "添加用户银行卡失败");
			
			result.code = -1;
			result.msg = "添加用户银行卡失败";
			return result;
		}
		result.code = 1;
		result.msg = "添加用户银行卡成功";
		return result;
	}
	
	/**
	 * 删除用户银行卡
	 *
	 * @param id
	 * @return
	 *
	 * @author Chenzhipeng
	 * @createDate 2015年12月23日
	 */
	public int delUserCard(long id) {
		
		return bankCardUserDao.delete(id);
	}


	/**
	 * 删除绑定银行卡
	 *
	 * @param userId
	 *
	 * @author huangyunsong
	 * @createDate 2016年2月15日
	 */
	public void delBank(long userId) {
		String sql = "DELETE FROM t_bank_card_user WHERE user_id = :userId";
		Map<String,Object> args = new HashMap<String, Object>();
		args.put("userId", userId);
		bankCardUserDao.deleteBySQL(sql, args);
	}

	/**
	 * 修改用户银行卡状态
	 *
	 * @param id
	 * @param userId
	 * @return
	 *
	 * @author Chenzhipeng
	 * @createDate 2015年12月23日
	 */
	public int updateUserCardStatus(long id, long userId) {

		return bankCardUserDao.updateCardStatus(id, userId);
	}

	/**
	 * 查询用户绑定银行卡
	 * @description 
	 *
	 * @author Chenzhipeng
	 * @createDate 2015年12月25日
	 * @param userId
	 * @return
	 */
	public List<t_bank_card_user> queryCardByUserId(long userId) {
		
		return bankCardUserDao.findListByColumn("user_id = ? ORDER BY time", userId);
	}
	
	/**
	 * 查询用户绑定单张银行卡
	 * @description 
	 *
	 * @author Chenzhipeng
	 * @createDate 2015年12月25日
	 * @param userId
	 * @return
	 */
	public t_bank_card_user querySingleCardByBankAccount(String bankAccount) {
		
		return bankCardUserDao.findByColumn("account = ?", bankAccount);
	}
	
	/***
	 * 查询用户默认卡号
	 * @param userId
	 * @return
	 * @description 
	 *
	 * @author luzhiwei
	 * @createDate 2016-5-10
	 */
	public String queryDefaultCardAccount(long userId){
		Map<String,Object> args = new HashMap<String, Object>();
		args.put("userId", userId);
		args.put("status", Status.TYPE_DEFAULT.code);

		return bankCardUserDao.findSingleStringBySQL("SELECT account FROM t_bank_card_user WHERE status =:status AND user_id =:userId ", null, args);
	}

	/**
	 * 更新用户银行卡信息
	 * @param bankName
	 * @param bankCode
	 * @param bankAccount
	 * @param userId
	 * @return
	 */
	public int updateUserBankInfo(String bankName, String bankCode, String bankAccount, long userId) {
		String sql = "update t_bank_card_user set bank_name=:bankName, bank_code=:bankCode, account=:account where user_id=:userId and status=:status";
		
		Map<String, Object> args = new HashMap<String, Object>();
		args.put("bankName", bankName);
		args.put("bankCode", bankCode);
		args.put("account", bankAccount);
		args.put("userId", userId);
		args.put("status", t_bank_card_user.Status.TYPE_DEFAULT.code);
		
		return bankCardUserDao.executSQL(sql, args);
	}

	/**
	 * 添加银行卡数据校验  -app
	 * 
	 * @param bankName
	 * @param bankCode
	 * @param account
	 * @return
	 * @author zhiliang.lan
	 * @createDate 2017年8月4日
	 */
	public ResultInfo validateData(String bankName, String bankCode,
			String account) {
		ResultInfo result = new ResultInfo();
		
		if(StringUtils.isBlank(bankName)){
			result.code = -1;
			result.msg = "请填写银行卡名称";
			return result;
		}
		if(StringUtils.isBlank(bankCode)){
			result.code = -1;
			result.msg = "请填写银行卡代号";
			return result;
		}
		if(StringUtils.isBlank(account)){
			result.code = -1;
			result.msg = "请填写银行卡账号";
		}
		
		if (account.length() > 24 || account.length() < 9) {
			result.code=-1;
			result.msg="请填写正确的银行卡账号";
			return result;
		}
		
		if (bankName.length() > 32 || bankName.length() < 4 ) {
			result.code=-1;
			result.msg="请填写正确的银行卡名称";
			return result;
		}
		result.code = 1;
		result.msg = "校验成功";
		
		return result;
	}
	
	/**
	 * 根据userId查询银行卡列表   -app
	 * @param userId
	 * @return
	 * @author zhiliang.lan
	 * @createDate 2017年8月4日
	 */
	public List<Map<String, Object>> findBankCardByUserId(long userId) {
		
		return bankCardUserDao.findBankCardByUserId(userId);
	}

	/**
	 *
	 * @param bankAccount
	 * @return
	 * @author xiaojun
	 *@version 2017-8-26 
	 */
	public t_bank_card_user findBankCardUserByBankAccount(String bankAccount) {
		return bankCardUserDao.findBankCardUserByBankAccount(bankAccount);
	}

	/**
	 *	根据用户id和银行卡号查询
	 * @param id
	 * @param bankAccount
	 * @return
	 * @author Liangqiongfeng
	 *@version 2018-3-29 
	 */
	public t_bank_card_user findBankByUserIdByBankAccount(long user_id,
			String bankAccount) {
		return bankCardUserDao.findBankByUserIdByBankAccount(user_id,bankAccount);
	}

	
}
